<?php

namespace app\controller;

use app\general\ApiResponse;
use app\general\utils\ConversionUtils;
use app\model\MovieVideo;
use app\model\Video;
use support\Db;
use support\Request;

class MovieVideoController extends BaseController
{
    public function index(Request $request)
    {

        //$users = Db::table('movie_videos')->where('movie_id',$request->get('movieId'))->paginate(10);
        $users = MovieVideo::with('video')->where('movie_id',$request->get('movieId'))->paginate(10);

        $items = $users->items();

        foreach ($items as $key=>$val){
            $items[$key] = ConversionUtils::underscoreToHump($val->toArray()) ;
            //、、$items[$key]['video'] = ConversionUtils::underscoreToHump(Video::find($val-)->toArray()) ;
        }
        return  json($this->success('操作成功',$items,$users->total()));
    }

    /**
     * 检查验证码
     */
    public function show(Request $request,$id)
    {
        $data=  MovieVideo::find($id);
        if(empty($data)){
            return json($this->success('操作成功',[]));
        }
        $data = $data->toArray();
        $data=ConversionUtils::underscoreToHump($data) ;
        //$data['video']

        return json($this->success('操作成功',$data));
    }

    /**
     * 创建
     * @param Request $request
     * @return \support\Response
     */
    public function store(Request $request)
    {
        $MovieVideo = new MovieVideo();
        $MovieVideo->setRawAttributes(ConversionUtils::humpToUnderscore($request->all()));
        $MovieVideo->version=0;
        $MovieVideo->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 更新
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function update(Request $request,$id)
    {
        $MovieVideo = MovieVideo::find($id);
        if(empty($MovieVideo)){
            return  json($this->success('操作失败'));
        }
        $data = $request->all();
        $data = ConversionUtils::humpToUnderscore($data);
        unset($data['create_time'],$data['updateTime']);
        $MovieVideo->setRawAttributes($data);
        $MovieVideo->save();
        return  json($this->success('操作成功'));
    }

    /**
     * 删除
     * @param Request $request
     * @param $id
     * @return \support\Response
     */
    public function destroy(Request $request, $id)
    {
        MovieVideo::destroy($id) ;
        return  json($this->success('操作成功'));
    }

}
